<template>
  <div class="main-content">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="策略编码">
              <a-input placeholder="请输入策略编码" v-model="queryParam.ployCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增预警策略</a-button>
    </div>

    <!-- table区域-begin -->
    <div style="margin-top:10px;">
      <a-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
           <span slot="customTitle">策略编码
              <a-tooltip
                placement="topLeft"
                title="指预警策略历史配置记录"
              >
                <a-icon type="question-circle" />
              </a-tooltip>
           </span>
           <span slot="customStatusTitle">状态
              <a-tooltip
                placement="topLeft"
                title="当列表中只能有一条预警策略编码状态开启，其他的预警策略编码状态都显示关闭；"
              >
                <a-icon type="question-circle" />
              </a-tooltip>
           </span>
          <template slot="status" slot-scope="text,record">
            <a-switch checked-children="开" un-checked-children="关" :checked="text==1" @change="checked => handleChangeStatus(checked,record.id)" />
          </template>
          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </span>
      </a-table>
    </div>
  </div>
</template>

<script>
  import {publicApi,policyConfigEdit} from '@/api/warning/Policy.js'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  export default {
    mixins:[JeecgListMixin],
    data () {
      return {
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key: 'rowIndex',
            // width: 100,
            // align: 'center',
            customRender: (text, record, index) =>
              `${(this.ipagination.current - 1) * this.ipagination.pageSize +
                (index + 1)}` //序号分页后连续
          },
          {
          //   title: "策略编码",
            dataIndex: "ployCode",
            scopedSlots: { title: 'customTitle'  }
          },
          {
            // title:'状态',
            // align:"center",
            dataIndex: 'status',
            scopedSlots: { customRender: "status" ,title: 'customStatusTitle'  }
          },
          {
            title:'策略账号',
            // align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'配置时间',
            // align:"center",
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            // width:200,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: publicApi.policyConfigList,
          delete: publicApi.policyConfigDelete,
        },
      }
    },
    async created(){
    },
    methods: {
      handleAdd(){
        // this.$emit('handleAdd')//不用路由菜单做
        this.$router.push('/earlyWarning/strategy/policyConfig/add')
      },
      handleEdit(record){
        this.$router.push({
					path: "/earlyWarning/strategy/policyConfig/edit",
					query: {
						id: record.id
					}
				});
      },
      handleChangeStatus(checked,id){
        let params = {
          id:id,
          status:checked?'1':'0'
        }
        policyConfigEdit(params).then((res) => {
          if (res.success) {
            this.loadData()
          }else{
            this.$message.warning(res.message?res.message:'请求失败！')
          }
        }).catch(e=>{
            this.$message.warning("请求失败！");
        }).finally(() => {
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .title {
    color: rgba(0,0,0,.85);
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 16px;
  }
  .main-content{
    background: #fff;
    padding: 24px;
  }
</style>